package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.ReserveChildVH;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 委托房源适配器
 * Created by ruanzb on 2016/5/8.
 */
public class EntrustOriginAdapter extends BaseAdapter {

    private DrawableRequestBuilder<String> requestBuilder;
    private ArrayList<PostDo> postDoList;
    private LayoutInflater inflater;

    public EntrustOriginAdapter(Context context, DrawableRequestBuilder<String> requestBuilder, ArrayList<PostDo> postDoList) {
        this.requestBuilder = requestBuilder;
        this.postDoList = postDoList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return postDoList.size();
    }

    @Override
    public Object getItem(int position) {
        return postDoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        PostDo postDo = postDoList.get(position);
        ReserveChildVH holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_post_list, parent, false);
            holder = new ReserveChildVH(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ReserveChildVH) convertView.getTag();
        }
        if ("s".equalsIgnoreCase(postDo.getPostType())) {//二手房
            GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_logo));
            holder.atv_title.setText(postDo.getTitle());
            holder.atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %.2f平  %s",
                    postDo.getBedroomCount(),
                    postDo.getSittingRoomCount(),
                    postDo.getSize(),
                    postDo.getDirection()));
            holder.atv_tag_thi.setVisibility(View.GONE);
            holder.atv_tag_sec.setText(postDo.getCnEstate());
            holder.atv_price.setText(PriceUtil.salePrice(postDo.getPrice()));
            holder.atv_distance.setVisibility(View.GONE);
            holder.customListTagView.setTags(postDo.getKeyWords());
            holder.customListTagView.setVisibility(View.VISIBLE);
        } else {//租房
            GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_logo));
            holder.atv_title.setText(postDo.getTitle());
            holder.atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %s",
                    postDo.getBedroomCount(),
                    postDo.getSittingRoomCount(),
                    postDo.getDirection()));
            holder.atv_tag_sec.setText(postDo.getFitMent());
            holder.atv_tag_thi.setText(String.format(Locale.CHINA, "%s  %s", postDo.getGscope(), postDo.getCnEstate()));
            holder.atv_tag_thi.setVisibility(View.VISIBLE);
            holder.customListTagView.setVisibility(View.GONE);
            holder.atv_price.setText(PriceUtil.rentPrice(postDo.getRental()));
            holder.atv_distance.setVisibility(View.GONE);
            holder.customListTagView.setVisibility(View.GONE);
        }
        return convertView;
    }
}
